package com.example.petstore.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.petstore.entity.Collection;
import com.example.petstore.entity.Order;
import com.example.petstore.entity.VO.CollectionVO;
import com.example.petstore.entity.VO.OrderVO;
import com.example.petstore.exception.BaseException;
import com.example.petstore.mapper.OrderMapper;
import com.example.petstore.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.petstore.utils.BaseContext;
import com.example.petstore.utils.Result;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DD
 * @since 2024-07-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Override
    public Result getAll(int current, int size) {
        QueryWrapper<Order> collectionQueryWrapper = new QueryWrapper<>();

        OrderVO orderVO = new OrderVO();

        Page<Order> orderPage = new Page<>(current, size);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.eq(Order::getCreateTime, BaseContext.getCurrentId());
        this.page(orderPage,queryWrapper);

//        BeanUtils.copyProperties(collectionPage,,"records");

        List<Order> records = orderPage.getRecords();


        orderVO.setList(records);
        return Result.success(orderVO);
    }

    @Override
    public Result updateStatus(Order order) {
        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.eq("purchaser_id",BaseContext.getCurrentId());
        orderUpdateWrapper.eq("id",order.getId());
        orderUpdateWrapper.set("is_receive",order.getIsReceive());
        orderUpdateWrapper.set("is_pay ",order.getIsPay());
        orderUpdateWrapper.set("is_ship",order.getIsShip());
        boolean update = this.update(orderUpdateWrapper);

        if (!update){
            throw new BaseException("修改失败");
        }
        return  Result.success("修改成功");
    }

    @Override
    public Result purchase(Order order) {
        Order byId = this.getById(order.getId());
        byId.setIsPay(1);
        this.updateById(byId);

        return Result.success("购买成功");
    }

    @Override
    public Result receive(Order order) {
        Order byId = this.getById(order.getId());
        byId.setIsReceive(1);
        this.updateById(byId);

        return Result.success("接收成功");
    }

    @Override
    public Result create(Order order) {
        order.setPurchaserId(BaseContext.getCurrentId().toString());
        this.save(order);

        return Result.success("创建成功");
    }

    @Override
    public Result buyAll(List<Collection> collectionList) {
        List<Order> collect = collectionList.stream().map((item) -> {
            Order order = new Order();
//            BeanUtils.copyProperties(item,order,"id","create_time","update_time","is_delete");
            order.setImage(item.getStoreImage());
            order.setType(item.getShopType());
            order.setPurchaserId(item.getPurchaserId());
            order.setPrice(item.getPrice());
            return order;
        }).collect(Collectors.toList());

        boolean b = this.saveBatch(collect);
        if (!b){
            throw new BaseException("购买失败");
        }

        return Result.success("购买成功");
    }


    @Override
    public Result getNotPaid(int current, int size) {
        QueryWrapper<Order> collectionQueryWrapper = new QueryWrapper<>();

        OrderVO orderVO = new OrderVO();

        Page<Order> orderPage = new Page<>(current, size);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getCreateTime, BaseContext.getCurrentId());
        queryWrapper.eq(Order::getIsPay, 0);
        this.page(orderPage,queryWrapper);

//        BeanUtils.copyProperties(collectionPage,,"records");

        List<Order> records = orderPage.getRecords();


        orderVO.setList(records);
        return Result.success(orderVO);
    }
}
